package com.github.willisaway.core.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class NumberUtil {
    final static DecimalFormat df0 = (DecimalFormat) DecimalFormat.getInstance();
    final static DecimalFormat df2 = (DecimalFormat) DecimalFormat.getInstance();
    final static DecimalFormat df3 = (DecimalFormat) DecimalFormat.getInstance();
    static {
        df0.applyPattern("0");
        df2.applyPattern("0.00");
        df3.applyPattern("0.000");
    }

    public static String decimalFormat0(double dData) {
        return df0.format(dData);
    }

    public static String decimalFormat2(double dData) {
        return df2.format(dData);
    }

    public static String decimalFormat3(double dData) {
        return df3.format(dData);
    }

    public static double parseDouble(String data) {
        double result = 0.0;
        try {
            if (data == null || data.trim().length() == 0)
                result = Double.valueOf(0.0);
            else
                result = Double.valueOf(data.trim());
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
            System.out.print(data);
        }
        return result;
    }

    public static String formatNumber(Object arg) {
        DecimalFormat decimalFormat = new DecimalFormat("#######################.###########");
        return decimalFormat.format(arg);
    }

    public static void main(String[] args) {
        System.out.println(NumberUtil.formatNumber(BigDecimal.valueOf(1.23)));
        System.out.println(NumberUtil.formatNumber(BigDecimal.valueOf(1)));
        System.out.println(NumberUtil.formatNumber(BigDecimal.valueOf(1.0)));
        System.out.println(NumberUtil.formatNumber(BigDecimal.valueOf(10)));
        System.out.println(NumberUtil.formatNumber(1.23));
        System.out.println(NumberUtil.formatNumber(1));
        System.out.println(NumberUtil.formatNumber(1.0));
        System.out.println(NumberUtil.formatNumber(10));
        System.out.println(BigDecimal.valueOf(1.73).intValue());
        Number number = BigDecimal.valueOf(1.73);
//		System.out.println(number.);
    }
}
